import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.graph.MutableVertex;
import com.arcadedb.graph.Vertex;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;
import indi.hjhk.log.Logger;

import java.util.Optional;

public class QueryTest2 {
    public static void main(String[] args){
        Logger logger = Logger.getPureLogger();
        logger.logSilent("start");
        DatabaseFactory dbFactory= new DatabaseFactory("./databases/test5");
        try(Database db=dbFactory.open()) {

//        db.begin();
//        MutableVertex testcase = db.newVertex("SnapShotTest");
//        testcase.set("id", "testCase");
//        testcase.set("carId", null);
//        testcase.set("predId", null);
//        testcase.set("succId", null);
//        testcase.set("averageSpeed", 0);
//        testcase.set("currentSpeed", 0);
//        testcase.set("lastPeriodTime", 0);
//        testcase.set("lastPeriodMinMile", 0);
//        testcase.set("TimeExpired", 0);
//        testcase.set("timestamp", 0);
//        testcase.set("expired", 0);
//        testcase.set("passid", null);
//        testcase.save();
//        db.commit();

            long start, elapsed;

//        start=System.currentTimeMillis();
//        ResultSet rs= db.query("SQL", "SELECT FROM SnapShotTest WHERE timeStamp<=0 OR expired<=0");
//        System.out.println(rs.countEntries());
////        while (rs.hasNext()){
////            Result result=rs.next();
////            System.out.printf("id: %s\n", result.getProperty("id").toString());
////        }
//
//        elapsed=System.currentTimeMillis()-start;
//        System.out.println(elapsed+"ms");

            start = System.currentTimeMillis();
            ResultSet rs = db.query("SQL", "SELECT FROM SnapShot WHERE predId IS NULL AND succId IS NULL");
            logger.logOnStdout("SQL executed, printing results...");

            logger.disableAutoFlush();
            while (rs.hasNext()){
                Result result=rs.next();
                Optional<Vertex> optVSnapshot=result.getVertex();
                if (optVSnapshot.isEmpty()) continue;
                Vertex vSnapshot = optVSnapshot.get();
                logger.logSilent("%s(predId=%d, succId=%d)", vSnapshot.get("id"), vSnapshot.get("predId"), vSnapshot.get("succId"));
            }
            logger.flush();
            logger.enableAutoFlush();

            elapsed = System.currentTimeMillis() - start;
            logger.logOnStdout("time used: %dms", elapsed);
        }
    }
}
